﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ELab.Serialization.Data;

namespace ELab.Controls
{
    public partial class SurveyControl : UserControl
    {
        public event EventHandler AnswerSurveySuccess;
        public event EventHandler AnswerSurveyFalse;

        public SurveyControl()
        {
            InitializeComponent();
        }

        public void Clear()
        {
            grbSurvey.Controls.Clear();
        }
        private Survey[] mySurveys;
        private Survey currentSurvey;
        private int currentIndex = -1;
        public void ShowSurvey(Survey[] surveys)
        {
            if (surveys == null || surveys.Length == 0)
            {
                this.FindForm().Close();
                return;
            }

            if (this.InvokeRequired)
            {
                ShowSurvey(surveys);
            }
            else
            {
                mySurveys = surveys;
                if (currentIndex <= mySurveys.Length - 1)
                {
                    currentIndex++;
                    currentSurvey = surveys[currentIndex];
                    txtSurveyAnswer.Text = string.Empty;
                    lblSurveyQuestion.Text = currentSurvey.Question;
                }
                else
                    currentSurvey = null;
            }
        }

        private void btnNext_Click(object sender, EventArgs e)
        {
            if (currentSurvey == null || currentSurvey.Answer == null) return;
            if (txtSurveyAnswer.Text.Trim().ToUpper() == currentSurvey.Answer.Trim().ToUpper())
            {
                if (AnswerSurveySuccess != null && currentSurvey == mySurveys[mySurveys.Length - 1])
                {
                    AnswerSurveySuccess(null, null);
                    return;
                }
                ShowSurvey(mySurveys);
            }
        }
    }
}
